package com.randy.recyclerviews.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author xudong
 * @since 2017/2/10
 */

public class ExpandableList {
    @Nullable
    private final UniversalAdapter.AbstractModel headerModel, emptyViewModel, footerModel;
    @NonNull
    private final List<UniversalAdapter.AbstractModel<?>> childModels = new ArrayList<>();

    private boolean collapsed ;

    public boolean isCollapsed() {
        return collapsed;
    }

    public void setCollapsed(boolean collapsed) {
        this.collapsed = collapsed;
    }

    public ExpandableList() {
        this(null, null, null);
    }

    public ExpandableList(@Nullable UniversalAdapter.AbstractModel headerModel) {
        this(headerModel, null, null);
    }

    public ExpandableList(@Nullable UniversalAdapter.AbstractModel headerModel,
                          @Nullable UniversalAdapter.AbstractModel emptyViewModel,
                          @Nullable UniversalAdapter.AbstractModel footerModel) {
        this.headerModel = headerModel;
        this.emptyViewModel = emptyViewModel;
        this.footerModel = footerModel;
    }

    @Nullable
    public UniversalAdapter.AbstractModel getHeaderModel() {
        return headerModel;
    }

    @Nullable
    public UniversalAdapter.AbstractModel getEmptyViewModel() {
        return emptyViewModel;
    }

    @Nullable
    public UniversalAdapter.AbstractModel getFooterModel() {
        return footerModel;
    }

    @NonNull
    public List<UniversalAdapter.AbstractModel<?>> getChildModels() {
        return childModels;
    }

    public int size() {
        int size = 0;
        if (headerModel != null) {
            size++;
        }
        if (childModels.size() == 0) {
            if (emptyViewModel != null) {
                size++;
            }
        } else {
            size += childModels.size();
        }
        if (footerModel != null) {
            size++;
        }
        return size;
    }

    @NonNull
    public Collection<? extends UniversalAdapter.AbstractModel<?>> flatten() {
        List<UniversalAdapter.AbstractModel<?>> all = new ArrayList<>();
        if (headerModel != null) {
            all.add(headerModel);
        }
        if (childModels.size() == 0 || collapsed) {
            if (emptyViewModel != null) {
                all.add(emptyViewModel);
            }
        } else {
            all.addAll(childModels);
        }
        if (footerModel != null) {
            all.add(footerModel);
        }
        return all;
    }
}
